<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="app">
        <el-container style="height: 100vh; display: flex; justify-content: center; align-items: center;">
            <el-card style="width: 400px;">
                <h2 style="text-align: center; margin-bottom: 30px;">管理员登录</h2>
                <el-form :model="loginForm" :rules="rules" ref="loginForm">
                    <el-form-item prop="username">
                        <el-input
                            v-model="loginForm.username"
                            placeholder="请输入用户名"
                            @keyup.enter.native="login">
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input
                            v-model="loginForm.password"
                            type="password"
                            placeholder="请输入密码"
                            @keyup.enter.native="login">
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            type="primary"
                            style="width: 100%;"
                            @click="login"
                            :loading="loading">
                            登录
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </el-container>
    </div>
    <script>
        // 添加请求拦截器
        axios.interceptors.request.use(config => {
            const token = localStorage.getItem('token') || '';
            if (token) {
                config.headers.Authorization = `Bearer ${token}`;
            }
            config.withCredentials = true;
            return config;
        }, error => {
            return Promise.reject(error);
        });

        new Vue({
            el: '#app',
            data() {
                return {
                    loading: false,
                    loginForm: {
                        username: '',
                        password: ''
                    },
                    rules: {
                        username: [
                            { required: true, message: '请输入用户名', trigger: 'blur' }
                        ],
                        password: [
                            { required: true, message: '请输入密码', trigger: 'blur' }
                        ]
                    }
                }
            },
            methods: {
                login() {
                    this.$refs.loginForm.validate(valid => {
                        if (!valid) return;

                        this.loading = true;
                        axios.post('/login', {
                            username: this.loginForm.username,
                            password: this.loginForm.password
                        }, { withCredentials: true })
                        .then(response => {
                            localStorage.setItem('token', response.data.data.token);
                            window.location.href = '/';
                        })
                        .catch(error => {
                            this.$message.error(error.response?.data?.message || '登录失败');
                            this.loading = false;
                        });
                    });
                }
            }
        });
    </script>
</body>
</html>